package com.kgc.crowd.distributioincrowdwebui.controller;

import com.kgc.crowd.distributioncrowdcommon.api.MemberManagerRemoteService;
import com.kgc.crowd.distributioncrowdcommon.pojo.ResultEntity;
import com.kgc.crowd.distributioncrowdcommon.util.CrowdConstant;
import com.kgc.crowd.distributioncrowdcommon.vo.MemberSignSuccessVO;
import com.kgc.crowd.distributioncrowdcommon.vo.MemberVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 项目名：distribution-crowd-parent
 * 创建人：Junyizm
 * 创建时间：2020/4/2-11:11
 */
@RequestMapping("/member/manager")
@Controller
public class MemberController {
    @Resource
    MemberManagerRemoteService memberManagerRemoteService;

    @RequestMapping("/do/login")
    public String login(MemberVO memberVO, Model model, HttpSession session) {
        String loginacct = memberVO.getLoginacct();
        String userpswd = memberVO.getUserpswd();
        ResultEntity<MemberSignSuccessVO> login = memberManagerRemoteService.login(loginacct,userpswd);
        if(login.getResult().equals(ResultEntity.FAILED)){
            model.addAttribute(CrowdConstant.ATTR_NAME_MESSAGE,CrowdConstant.MESSAGE_LOGIN_FAILED);
            return "login";
        }
        MemberSignSuccessVO memberSignSuccessVO = login.getData();
        session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER,memberSignSuccessVO);
        return "member";
    }


    @RequestMapping("/logout")
    public String logout(HttpSession session){
        MemberSignSuccessVO memberSignSuccessVO =(MemberSignSuccessVO) session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
        if (memberSignSuccessVO == null){
            return "redirect:/index";
        }
        String token = memberSignSuccessVO.getToken();
        ResultEntity<String> logout = memberManagerRemoteService.logout(token);
        if (ResultEntity.FAILED.equals(logout.getResult())){
            throw new RuntimeException(logout.getMessage());
        }
        session.invalidate();
        return "redirect:/index";
    }

    @RequestMapping("/send/code")
    public ResultEntity<String> sendCode(String phoneNum){
        ResultEntity<String> resultEntity = memberManagerRemoteService.sendCode(phoneNum);
        return resultEntity;
    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public String register(MemberVO memberVO,Model model){
        ResultEntity<String> register = memberManagerRemoteService.register(memberVO);
        if(register.getResult() == ResultEntity.FAILED){
            model.addAttribute(CrowdConstant.ATTR_NAME_MESSAGE,register.getMessage());
            return "/reg";
        }
        return "login";
    }

    @RequestMapping("/minecrowdfunding")
    public String minecrowdfunding(){
        return "minecrowdfunding";
    }

    @RequestMapping("/member")
    public String member(){
        return "member";
    }
}
